Examples in <cinttypes> header
Example 1:
C++14
// C++ Program to demonstrate the use of the <cinttypes> // Header. #include <cinttypes> #include <iostream> int main() { std::int64_t myInt = 1234567890123456; std::cout << "My integer: %" PRId64 "\n" , myInt; return 0; } |
Output:
My integer: 1234567890123456
Explanation
To display a message on the console using an int64_t variable, we use the PRId64 format specifier preceded by the % sign. It is defined in the cinttypes header and instructs the formatting of the output implemented by the cout object. The message is printed through the use of the std::cout object.
Example 2: In this code, we will make use of the above syntax
C++
// C++ Program to demonstrate the use of the <cinttypes> // Header. #include <cinttypes> #include <iostream> int main() { uint8_t myByte = 0xFF; std:: printf ( "My byte: %" PRIu8 "\n" , myByte); return 0; } |
Output:
My byte: 255
Explanation
In this example, a value of 255, signified by 0xFF, is assigned to a variable called myByte during initialization. The PRIu8 format specifier, created for showcasing unsigned 8-bit integers, is used in conjunction with the printf function to display the value of myByte.
C++ 11 – Header
The header offers type aliases and functions for manipulating integer types with particular sizes and signedness, as a part of the C++11 standard. Its aim is to present a standard group of integer types across diverse platforms and architectures.